package com.sign2024.sign2024project.projectservice.controller.ExcelType;

import com.alibaba.excel.write.handler.AbstractRowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.sign2024.sign2024project.projectservice.vo.GCProgectVo;
import org.apache.poi.ss.usermodel.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 自定义写处理器，实现单元格合并
public class GCProgectWriteHandlerColour extends AbstractRowWriteHandler {
    private final List<GCProgectVo> dataList;
    //    private final Map<String, Integer> mergeMap = new HashMap<>();
    // 技术分组
    private final Map<String, Integer> skillClassMap = new HashMap<>();

    private final Map<String, Integer> bossClassMap = new HashMap<>();

    // 颜色列表  PALE_BLUE  CORNFLOWER_BLUE  LIGHT_CORNFLOWER_BLUE  浅矢车菊蓝
    private static final IndexedColors[] COLOR_LIST = new IndexedColors[]{IndexedColors.LIGHT_GREEN,
            IndexedColors.LIGHT_CORNFLOWER_BLUE, IndexedColors.LIGHT_TURQUOISE, IndexedColors.CORNFLOWER_BLUE};
    // 当前颜色索引
    private int currentColorIndex = 0;

    private static final IndexedColors[] COLOR_LIST_Two = new IndexedColors[]{IndexedColors.PALE_BLUE, IndexedColors.AQUA};
    // 当前颜色索引
    private int currentColorBossIndex = 0;

    public GCProgectWriteHandlerColour(List<GCProgectVo> dataList) {
        this.dataList = dataList;
        calculateMergeRanges();
    }

    int count = 0;

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
        if (isHead) {
            return;
        }

        // 技术分组
        String skillClassValue = dataList.get(relativeRowIndex).getSkillClass();
        if (skillClassValue != null && skillClassMap.containsKey(skillClassValue)) {
            int firstRow = findFirstRowForMerge(skillClassValue, dataList, "skillClass");
            int lastRow = firstRow + skillClassMap.get(skillClassValue) - 1;
//            if (lastRow > firstRow) {
//                if (relativeRowIndex == lastRow - 1) {
//                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 10, 10));
//                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 11, 11));
//                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 12, 12));
//                }
//            }

            if (relativeRowIndex == lastRow - 1) {
                applyColor(writeSheetHolder, firstRow, lastRow, 0, 1, 2, 3, 4, 5, 6,
                        7, 8, 9, 10, 11, 12);
            }
        }

        // 商务分组
        String bossClassValue = dataList.get(relativeRowIndex).getBossClass();
        if (bossClassValue != null && bossClassMap.containsKey(bossClassValue)) {
            int firstRow = findFirstRowForMerge(bossClassValue, dataList, "bossClass");
            int lastRow = firstRow + bossClassMap.get(bossClassValue) - 1;
//            if (lastRow > firstRow) {
//                if (relativeRowIndex == lastRow - 1) {
//                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 15, 15));
//                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 16, 16));
//                    writeSheetHolder.getSheet().addMergedRegion(new CellRangeAddress(firstRow, lastRow, 17, 17));
//                }
//            }

            if (relativeRowIndex == lastRow - 1) {
                applyColorBoss(writeSheetHolder, firstRow, lastRow, 13, 14, 15, 16, 17);
            }
        }
    }

    private void calculateMergeRanges() {
        for (int i = 0; i < dataList.size(); i++) {
            GCProgectVo item = dataList.get(i);
            if (item.getSkillClass() != null) {
                skillClassMap.put(item.getSkillClass(), skillClassMap.getOrDefault(item.getSkillClass(), 0) + 1);
            }
            if (item.getBossClass() != null) {
                bossClassMap.put(item.getBossClass(), bossClassMap.getOrDefault(item.getBossClass(), 0) + 1);
            }
        }
    }

    private int findFirstRowForMerge(String value, List<GCProgectVo> dataList, String property) {
        for (int i = 0; i < dataList.size(); i++) {
            GCProgectVo item = dataList.get(i);
            if (property.equals("skillClass") && value.equals(item.getSkillClass())) {
                return i + 1; // +1 因为行号从0开始
            } else if (property.equals("bossClass") && value.equals(item.getBossClass())) {
                return i + 1;
            }
        }
        return -1;
    }

    private void applyColor(WriteSheetHolder writeSheetHolder, int firstRow, int lastRow, int... columns) {
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(COLOR_LIST[currentColorIndex].getIndex());
        cellStyle.setFillPattern(FillPatternType.forInt((short) 1)); // 使用短整型值 1 表示solid填充


        // 设置边框颜色为黑色
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());

        // 设置文本内容居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 允许自动换行
        cellStyle.setWrapText(true);

        for (int col : columns) {
            for (int r = firstRow; r <= lastRow; r++) {
                Row currentRow = writeSheetHolder.getSheet().getRow(r);
                if (currentRow != null) {
                    Cell cell = currentRow.getCell(col);
                    if (cell != null) {
                        cell.setCellStyle(cellStyle);
                    } else {
                        // 如果单元格不存在，创建一个新的单元格并设置样式
                        cell = currentRow.createCell(col);
                        cell.setCellStyle(cellStyle);
                    }
                }
            }
        }


//        for (int col : columns) {
//            for (int r = firstRow; r <= lastRow; r++) {
//                Row currentRow = writeSheetHolder.getSheet().getRow(r);
//                if (currentRow != null) {
//                    currentRow.getCell(col).setCellStyle(cellStyle);
//                }
//            }
//        }
        currentColorIndex = (currentColorIndex + 1) % COLOR_LIST.length;
    }

    private void applyColorBoss(WriteSheetHolder writeSheetHolder, int firstRow, int lastRow, int... columns) {
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFillForegroundColor(COLOR_LIST_Two[currentColorBossIndex].getIndex());
        cellStyle.setFillPattern(FillPatternType.forInt((short) 1)); // 使用短整型值 1 表示solid填充

//        for (int col : columns) {
//            for (int r = firstRow; r <= lastRow; r++) {
//                Row currentRow = writeSheetHolder.getSheet().getRow(r);
//                if (currentRow != null) {
//                    currentRow.getCell(col).setCellStyle(cellStyle);
//                }
//            }
//        }


        // 设置边框颜色为黑色
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());

        // 设置文本内容居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 允许自动换行
        cellStyle.setWrapText(true);

        for (int col : columns) {
            for (int r = firstRow; r <= lastRow; r++) {
                Row currentRow = writeSheetHolder.getSheet().getRow(r);
                if (currentRow != null) {
                    Cell cell = currentRow.getCell(col);
                    if (cell != null) {
                        cell.setCellStyle(cellStyle);
                    } else {
                        // 如果单元格不存在，创建一个新的单元格并设置样式
                        cell = currentRow.createCell(col);
                        cell.setCellStyle(cellStyle);
                    }
                }
            }
        }

        currentColorBossIndex = (currentColorBossIndex + 1) % COLOR_LIST_Two.length;
    }
}